import { createStore } from 'vuex'

const store = createStore({
  state: {
    token: sessionStorage.getItem('token'),  // 登录凭证
    username: sessionStorage.getItem("username"),   // 登录用户名
    menus: JSON.parse(sessionStorage.getItem("menus") || '[]') ,  // 左侧菜单列表
    activePath: null  //当前显示页面路径信息, 对象结构
  },
  mutations: {
    setToken(state, value){
      state.token = value;
      sessionStorage.setItem("token", value)
    },
    setName(state, value){
      state.username = value;
      sessionStorage.setItem("username", value)
    },
    setMenus(state, value){
      state.menus = value
      sessionStorage.setItem("menus", JSON.stringify(value))
    },
    setPath(state, value){
      state.activePath = value;
      sessionStorage.setItem("activePath", JSON.stringify(value))
    }
  },
  actions: {
    getPath(store){
      let activePath = sessionStorage.getItem("activePath");
      if(activePath){
        activePath = JSON.parse(activePath)
      }else{
        activePath = {
          path: "/goods",
          name1: "商品管理",
          name2: "商品列表"
        }
      }
      store.commit("setPath", activePath)
    }
  }
})
store.dispatch("getPath")
export default store
